package com.health.httpbean.upload;

import android.util.Log;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UiMessageUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.health.httpbean.HttpConstant;
import com.health.httpbean.HttpDataBean;
import com.health.httpbean.HttpResultBean;
import com.health.httpbean.QueryTypeEnum;
import com.health.httpbean.format.FormatGxyData;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.CD_HYPERTENSIONFOLLOWUP;
import com.health.tools.AssetUtil;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.settings.DataDownLoadMainActivity;

import java.util.List;

import rxhttp.RxHttp;

/**
 * author : xiaoxin
 * date   : 2020/12/19 18:28
 * desc   :
 * update :
 */
public class GxyUpload {

    private static final String TAG = "GxyUpload";

    private DataDownLoadMainActivity activity;
    private int gxyFTotalSize = 0;
    private int gxyFSuccessSize = 0;
    private int gxyFErrorSize = 0;

    public void uploadGxy(DataDownLoadMainActivity activity) {
        this.activity = activity;
        Gson gson = new Gson();
        List<CD_HYPERTENSIONFOLLOWUP> gxyList = NovaApplication.fdbmanger.findAllByWhere(CD_HYPERTENSIONFOLLOWUP.class,
                "IS_UPDATE = 'Y'");
        if (gxyList.size() <= 0) {
            ToastUtils.showShort("高血压无随访数据可上传");
            UiMessageUtils.getInstance().send(2000,"高血压无随访数据可上传");
        }else {
            gxyFTotalSize = gxyList.size();
            UiMessageUtils.getInstance().send(2001,"高血压数据" + gxyList.size() + "条正在上传，请稍候");
            String json = AssetUtil.getJson("GxySymptom.json", NovaApplication.getInstance());
            List<BD_DD_INFO> list = GsonUtils.fromJson(json, new TypeToken<List<BD_DD_INFO>>() {}.getType());
            for (int i = 0 ; i < gxyList.size() ; i++) {
                HttpDataBean httpDataBean = new HttpDataBean();
                httpDataBean.QueryType = QueryTypeEnum.GXY.getCode();
                httpDataBean.Data = gson.toJson(FormatGxyData.formatGxy(gxyList.get(i),list));
                upLoadHttpGxy(GsonUtils.toJson(httpDataBean),gxyList.get(i),i);
            }
        }
    }

    private void upLoadHttpGxy(String json, CD_HYPERTENSIONFOLLOWUP model, final int position) {
        activity.showProgressDialog("高血压随访" + gxyFTotalSize + "条记录,正在上传");
        RxHttp.postJson(HttpConstant.getHttpUrlUpLoadData())
                .addAll(json)
                .asClass(HttpResultBean.class)
                .subscribe(resultBean -> {
                    if (resultBean.getResponseCode() == 200) {
                        gxyFSuccessSize++;
                        model.setIS_UPDATE("N");
                        upDataGxy(model);
                    } else {
                        gxyFErrorSize++;
                        ToastUtils.showShort("" + resultBean.getResponseMsg());
                    }
                    if (position == gxyFTotalSize - 1) {
                        Log.e(TAG, "upLoadHttp55: 2000");
                        UiMessageUtils.getInstance().send(2000,
                                "高血压随访数据上传成功" + gxyFSuccessSize+"条" + " 失败" + gxyFErrorSize + "条");
                    }
                }, throwable -> {
                    gxyFErrorSize++;
                    LogUtils.e("出错了");
                });
    }

    public static void upDataGxy(CD_HYPERTENSIONFOLLOWUP model) {
        NovaApplication.fdbmanger.updata(model, "IDCARD='" + model.getIDCARD() + "'");
    }

}
